package bs.app.android;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

public class CBase extends Activity {
	public Implement App = null;
	public static boolean isGotWH = false;
	public static boolean isStart = false;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//System.out.println(" ---> CREATE!");
//		strDebug = "onCreate()";
		//this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		App = new Implement(this, (new CView(this)));
		
	}
	
	/** Must call after the activity is first created. */
	public void _startMidlet(MIDlet midlet){
		System.out.println("khoi dong midlet");
		App._startApp(midlet);
		Static.App.setContentView(Static.view);
		Static.view.setFocusable(true);
		isStart = true;
	}
	
	@Override
	public void onSaveInstanceState(Bundle savedInstanceState) {
	  super.onSaveInstanceState(savedInstanceState);
//	  strDebug = "onSaveInstanceState()";
	}
	
	@Override
	public void onRestoreInstanceState(Bundle savedInstanceState) {
	  super.onRestoreInstanceState(savedInstanceState);
//	  strDebug = "onRestoreInstanceState()";
	}

	@Override
    public void onPause() {
		super.onPause();
		System.out.println(" ---> onPause!");
//		strDebug = "onPause()";
    	App.pause();
    }
    
	@Override
    public void onResume() {
		super.onResume();
		System.out.println(" ---> onResume!");
//		strDebug = "onResume()";
		App.start();
    }
	
	@Override
    protected void onRestart(){
		super.onRestart();
		System.out.println(" ---> onRestart!");
//		strDebug = "onRestart()";	
//		App.start();
    }

	@Override
    protected void onStop(){
		super.onStop();
		System.out.println(" ---> STOP!");
//		strDebug = "onStop()";
    }

	@Override
    protected void onDestroy(){
		System.out.println(" ---> DESTROY!");
//		strDebug = "onDestroy()";
		App.destroy();
		
		super.onDestroy();
		
		if(!isFinishing()){
			//System.out.println(" ---> TO BE DESTROY!");
			this.finish();
		}
		
		android.os.Process.killProcess(android.os.Process.myPid());   
		System.exit(0);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	// TODO Auto-generated method stub
    	 if (keyCode == KeyEvent.KEYCODE_BACK){
    		 App.keyDown(Static.convertKey(keyCode));
	    	 return false;
	    }	    	
    	return super.onKeyDown(keyCode, event);
    }
    
	private class CView extends View{
		CBase mainClass = null;

		public CView(Context context){
			super(context);
			mainClass = (CBase) context;
		}
		
		public boolean onTrackballEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                //BaseObject.sSystemRegistry.inputSystem.roll(event.getRawX(), event.getRawY());
            } else if (event.getAction() == MotionEvent.ACTION_DOWN) {
            	onKeyDown(KeyEvent.KEYCODE_DPAD_CENTER, null);
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
            	onKeyUp(KeyEvent.KEYCODE_DPAD_CENTER, null);
            }
	        return true;
	    }
	    
		@Override 
	    protected void onSizeChanged (int w, int h, int oldw, int oldh){
			super.onSizeChanged(w, h, oldw, oldh);
//			strDebug = "onSizeChanged()";
			isGotWH = true;
			mainClass.App.resize(w, h);
	    }
	    
	    public boolean onTouchEvent(MotionEvent event) {
	    	    	
	    	int x = 0, y = 0;
        	x = (int)event.getX();
        	y = (int)event.getY();
            if (event.getAction() == MotionEvent.ACTION_UP) {
            	//System.out.println("su kien tuoch up ");
            	mainClass.App.pointerReleased(x, y);
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            	//System.out.println("su kien tuoch move ");
            	mainClass.App.pointerDragged(x, y);
            } else {
            //	System.out.println("su kien tuoch");
            	mainClass.App.pointerPressed(x, y);
            }
	        return true;
	    }
	    
	    @Override
	    public boolean onKeyDown (int keyCode, KeyEvent event){
//            System.out.println("Nhan phim keyCode="+keyCode);
	    	mainClass.App.keyDown(Static.convertKey(keyCode));
//	    	if (keyCode == KeyEvent.KEYCODE_BACK){
//	    		System.out.println("KeyEvent.KEYCODE_BACK");
//	            return false; // return
//	    	}
	    	return super.onKeyDown(keyCode, event);	   
	    }
	    
	    @Override
	    public boolean onKeyUp (int keyCode, KeyEvent event){
//	    	System.out.println("Nha phim ra!");
	    	mainClass.App.keyUp(Static.convertKey(keyCode));
//	    	if (keyCode == KeyEvent.KEYCODE_BACK)
//	        {
//	    		System.out.println("KeyEvent.KEYCODE_BACK");
//	            //moveTaskToBack(true);
//	            return false; // return
//	        }
	        return super.onKeyUp(keyCode, event);
	    }
		
//	    public void onWindowFocusChanged (boolean hasWindowFocus){
//	    	super.onWindowFocusChanged(hasWindowFocus);
//	    	strDebug = "onWindowFocusChanged()";
//	    	if(!hasWindowFocus)
//	    		mainClass.App.pause();
//	    }
	    
		@Override 
		protected void onDraw(Canvas canvas) {
			
			//System.out.println("ve canvas");
			super.onDraw(canvas);
			mainClass.App.paint(canvas);
			//Debug
//			mainClass.App.g.setColor(0xFFFFFF);
//			mainClass.App.g.drawString(strDebug, 30, 30, 0);
		}
	}
	
	//private String strDebug = "";
}
